
const { ccclass, property } = cc._decorator;

@ccclass
export default class PreLoadCountManager {

  private static _instance: PreLoadCountManager = null;

  public static getInstance(): PreLoadCountManager {
    if (!PreLoadCountManager._instance) {
      PreLoadCountManager._instance = new PreLoadCountManager();
    }

    return PreLoadCountManager._instance;
  }

  private _nResTotalCount: number = 0;
  private _nResCurrLoadedCount: number = 0;
  private m_bHasPreload = false

  private m_DeltaTime = 0;
  private m_MaxDeltaTime = 5;

  public clearNum() {
    this._nResTotalCount = 0
    this._nResCurrLoadedCount = 0
  }

  public HasPreload() {
    // if(cc.sys.platform === cc.sys.WECHAT_GAME)
    //     return this.m_bHasPreload

    // return true

    return this.m_bHasPreload

  }

  public SetPreload() {
    this.m_bHasPreload = true
  }

  public setResCurrLoadedCount(num: number) {
    this._nResCurrLoadedCount = num;
    //console.log("###当前预加载完成数:",num);
  }

  public addTotalResCount() {
    ++this._nResTotalCount;
  }

  public addResCurrLoadedCount() {
    this._nResCurrLoadedCount++

    this.m_DeltaTime = 0;
  }

  /**
   * loading 当前预加载数量
   */
  public getResCurrLoadedCount(): number {
    return this._nResCurrLoadedCount;
  }

  public setResTotalCount(num: number) {
    this._nResTotalCount = num;
    //console.log("###预加载总数:",num);
  }
  /**
   * loading预加载资源总数
   */
  public getResTotalCount(): number {
    return this._nResTotalCount;
  }

  public getIsAllLoaded(): boolean {
    return this.getResCurrLoadedCount() >= this.getResTotalCount();
  }

  public getProgress(): number {
    return this._nResCurrLoadedCount / this._nResTotalCount;
  }

  public getProgressTxt(): string {

    return this._nResCurrLoadedCount + "/" + this._nResTotalCount;
  }

  //加载时间太长
  public isDelaytooLong(): boolean {
    if (cc.sys.platform === cc.sys.WECHAT_GAME) {
      if (this.m_DeltaTime >= this.m_MaxDeltaTime) {
        return true;
      }
    }
    return false;
  }

  public update(dt) {
    this.m_DeltaTime += dt;
  }
}